// pages/decovery/index.js
const common = require('../../../public.js')
const utils = require('../utils.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageSize: 10,
    pageNum: 1,
    articleList: [],
    canLoad: true,
    isLoadingBusy: false,
    refreshing: false,
    userId:"",
    token:"",
    tabs:'managerId',
    active:3
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onChangeTabbars(e) { //切换底部导航
    console.log(e)
    const index = e.detail.index
    const url = e.detail.url + '?fl=lanling' 
    console.log(url)
    if (index != this.data.active) {
      utils.onChangeTabbar(url, this.data.storeId, this.data.moduleType)
    }
  },
  onLoad: function (options) {
    console.log()

    let that = this
    wx.getStorage({
      key: 'user',
      success: function (res) {
        console.log(res.data.user.id)
        that.setData({
          userId:res.data.user.id,
          token:res.data.token
        })
      
      }
    })
    that.getStoreArticleList(true)
   
  },

  getStoreArticleList(refresh = false) {
    const that = this
  
    if (refresh) {
      this.data.canLoad = true
      this.setData({
        refreshing: true
      })
    }
    if (that.data.canLoad) { //可以加载
      if (!refresh && this.data.isLoadingBusy) {
        return
      }
      this.setData({
        isLoadingBusy: true
      })
      const pageNum = refresh ? 1 : this.data.pageNum + 1
      let tempJson = {
        userId:that.data.userId,
        regionId:common.regionId,
        pageSize: that.data.pageSize,
        pageNum
      }
      common.post('/wlplatfrom/storeArticle/getStoreArticleList', tempJson, function (res) {
        if (res.status === 200) {
          that.data.pageNum = pageNum
          if (res.data != '') {
            that.setData({
              articleList: refresh ? res.data : [...that.data.articleList, ...res.data]
            })
          } else {
            that.setData({
              canLoad: false,
            })
          }
        } else {
          common.showRequestErrMsg(res)
        }
      }, function () {
        that.setData({
          isLoadingBusy: false
        })
        if (refresh) {
          //停止下拉刷新
          wx.stopPullDownRefresh()
          that.setData({
            refreshing: false
          })
        }
      })
    }
  },
  toArticleDetail(e) {
    const item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pages/discovery/articleDetail/index?id=' + item.id
        + '&storeName=' + item.storeName
        + '&title=' + item.title
        + '&createTime=' + item.createTime
        + '&storeImg=' + item.storeImg
    })
  },
  onReachBottom() {
    this.getStoreArticleList(false)
  },
  onPullDownRefresh: function () {
    this.getStoreArticleList(true)
  },
})